package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.qhse.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.qhse.HazardSourceMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.qhse.HazardSource;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.qhse.HazardSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @version V1.0
 * @Package com.cqhilink.iot.integratedapplicationmanagement.services.innerservice.impl.qhse
 * @Description: ${TODO}(重大危险源服务具体实现)
 * @author: lutianbo
 * @date: 2016/11/29,0029
 */
public class HazardSourceServiceImpl implements HazardSourceService{
    @Autowired
    private HazardSourceMapper sourceMapper;
    @Autowired
    private IamFileService iamFileService;

    /**
     * 删除重大危险源
     * @param hazardSourceIds
     * @return
     */
    @Override
    public int deleteByPrimaryKey(String[] hazardSourceIds){
        if (hazardSourceIds.length>0){
            return  sourceMapper.deleteByPrimaryKey(hazardSourceIds);
        }
        return 0;
    }

    /**
     * 新增重大危险源
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles 需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(HazardSource record,String uploadFiles,String deletedFiles){
        // 数据保存成功之后，返回的条数
        int resultCount = 0;
        // 保存业务数据
        if (null == record){
            return resultCount;
        }
        resultCount += sourceMapper.insertSelective(record);
        // 附件保存
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getHazardSourceId());

        return resultCount;
    }

    /**
     * 根据id查询重大危险源
     * @param hazardSourceId
     * @return
     */
    @Override
    public HazardSource selectByPrimaryKey(String hazardSourceId){
        if(!"".equals(hazardSourceId)){
            HazardSource drill = sourceMapper.selectByPrimaryKey(hazardSourceId);
            return  drill;
        }
       return null;
    }

    /**
     * 根据id更新重大危险源
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles 需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(HazardSource record,String uploadFiles,String deletedFiles){
        // 更新数据成功之后，返回的条数
        int resultCount = 0;
        // 更新数据
        if(null == record){
            return resultCount;
        }
        // 保存数据
        resultCount += sourceMapper.updateByPrimaryKeySelective(record);
        // 附件更新
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getHazardSourceId());

        return resultCount;
    }

    /**
     * 重大危险源列表查询
     * @param searchBean
     * @return
     */
    @Override
    public JSONObject hazardSourceList(BootstrapSearchBean searchBean){
        // 返回结果
        JSONObject jsonObject = new JSONObject();
        // 分页设置
        searchBean.setLimit( searchBean.getLimit() == null ? Integer.valueOf(10) : searchBean.getLimit() );
        searchBean.setOffset( searchBean.getOffset() == null ? Integer.valueOf(0) : searchBean.getOffset() );
        // 总条数
        jsonObject.put("total",sourceMapper.queryCount(searchBean));
        jsonObject.put("rows", JSON.toJSON(sourceMapper.queryList(searchBean)));
        return jsonObject;
    }

    /**
     * 判断重大危险源名是否存在
     * @param hazardName 危险源名
     * @return true 存在 false 不存在
     */
    @Override
    public  boolean isExitByName(String hazardName){
        return sourceMapper.isExitByName(hazardName) > 0 ? true :false;
    }

}
